<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="科室名" prop="clinicName" label-width="60px">
        <el-input
          v-model="queryParams.clinicName"
          placeholder="请输入科室名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="当班医生姓名" prop="doctorName">
        <el-input
          v-model="queryParams.doctorName"
          placeholder="请输入当班医生姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开放时间" prop="openTime">
        <el-date-picker clearable
          v-model="queryParams.openTime"
          type="datetime"
          value-format="yyyy-MM-dd HH-mm-ss"
          placeholder="请选择开放时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="关闭时间" prop="closeTime">
        <el-date-picker clearable
          v-model="queryParams.closeTime"
          type="datetime"
          value-format="yyyy-MM-dd HH-mm-ss"
          placeholder="请选择关闭时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['medical:clinic:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['medical:clinic:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['medical:clinic:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['medical:clinic:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="clinicList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column v-if="columns[0].visible" label="医务室id" align="center" prop="clinicId" />
      <el-table-column v-if="columns[1].visible" label="科室名" align="center" prop="clinicName" />
      <el-table-column v-if="columns[2].visible" label="当班医生id" align="center" prop="doctorId" />
      <el-table-column v-if="columns[3].visible" label="当班医生姓名" align="center" prop="doctorName" />
      <el-table-column v-if="columns[4].visible" label="启用情况" align="center" prop="startStatus">
        <template slot-scope="scope">
          <span v-if="scope.row.startStatus === '0'">正常</span>
          <span v-if="scope.row.startStatus === '1'">关闭</span>
          <span v-if="scope.row.startStatus === '2'">维修</span>
        </template>
      </el-table-column>
      <el-table-column v-if="columns[5].visible" label="开放时间" align="center" prop="openTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.openTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="columns[6].visible" label="关闭时间" align="center" prop="closeTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.closeTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['medical:clinic:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['medical:clinic:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改医务室信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="科室名" prop="clinicName">
              <el-input v-model="form.clinicName" placeholder="请输入科室名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="当班医生" prop="doctorName">
              <el-select v-model="form.doctorName" placeholder="请选择当班医生">
                <el-option v-for="(item) in doctorOption" :key="item.doctorId" :label="item.doctorName"
                           :value="item.doctorName">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="开放时间" prop="openTime">
              <el-date-picker clearable
                              v-model="form.openTime"
                              type="datetime"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="请选择开放时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="关闭时间" prop="closeTime">
              <el-date-picker clearable
                              v-model="form.closeTime"
                              type="datetime"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="请选择关闭时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="启用状态" prop="startStatus">
              <el-select v-model="form.startStatus" placeholder="请选择启用状态">
                <el-option v-for="(item) in startStatusType" :key="item.type" :label="item.name"
                                     :value="item.type"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listClinic, getClinic, delClinic, addClinic, updateClinic } from "@/api/medical/clinic";
import {listDoctorList} from "../../../api/medical/doctor";

const startStatusType=[
  {type:'0',name:'正常'},
  {type:'1',name:'关闭'},
  {type:'2',name:'维修'},
]
export default {
  name: "Clinic",
  data() {
    return {
      // 遮罩层
      loading: true,
      startStatusType,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 医务室信息表格数据
      clinicList: [],
      doctorOption:[],
      columns:[
        {key:0,label:'医务室id',visible:false},
        {key:1,label:'科室名',visible:true},
        {key:2,label:'当班医生id',visible:false},
        {key:3,label:'当班医生姓名',visible:true},
        {key:4,label:'启用情况',visible:true},
        {key:5,label:'开放时间',visible:true},
        {key:6,label:'关闭时间',visible:true},
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        clinicName: null,
        doctorId: null,
        doctorName: null,
        startStatus: null,
        openTime: null,
        closeTime: null
      },
      queryDoctors:{
        doctorName: null,
        doctorSex: null,
        idCard: null,
        age: null,
        birthDate: null,
        currentAddress: null,
        native: null,
        phone: null,
        cultureAgree: null,
        enterTime: null,
        jobTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        clinicName:[
          {required:true, message:"名称不能为空",trigger:"blur"},
          {min: 2, max: 20, message:"名称大小应该在2-20个字符之间",trigger:"blur"},
        ],
        startStatus:[
          {required:true},
        ]
      },
      filters:{
        startStatusTypeFilter(type){
          const startStatusTy=startStatusType.find(obj=>obj.type===type);
          return startStatusTy ? startStatusTy.name : null;
        },
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询医务室信息列表 */
    getList() {
      this.loading = true;
      listClinic(this.queryParams).then(response => {
        this.clinicList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        clinicId: null,
        clinicName: null,
        doctorId: null,
        doctorName: null,
        startStatus: null,
        openTime: null,
        closeTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.clinicId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      listDoctorList(this.queryDoctors).then(res=>{
        this.doctorOption=res;
      });
      this.title = "添加医务室信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const clinicId = row.clinicId || this.ids
      getClinic(clinicId).then(response => {
        this.form = response.data;
        listDoctorList(this.queryDoctors).then(res=>{
          this.doctorOption=res;
        });
        this.open = true;
        this.title = "修改医务室信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.clinicId != null) {
            updateClinic(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addClinic(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const clinicIds = row.clinicId || this.ids;
      this.$modal.confirm('是否确认删除医务室信息编号为"' + clinicIds + '"的数据项？').then(function() {
        return delClinic(clinicIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('medical/clinic/export', {
        ...this.queryParams
      }, `clinic_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
